package com.cems.service.impl;/*
 *
 *@auth  wenzhao
 *@date 2021/5/17  19:52
 */

import com.cems.mapper.EntrustMapper;
import com.cems.mapper.UserMapper;
import com.cems.pojo.*;
import com.cems.pojo.ComUser;
import com.cems.pojo.SysAdmin;
import com.cems.pojo.SysUser;
import com.cems.service.UserService;
import com.cems.util.RedisUtil;
import com.cems.util.SelectRedis;
import com.cems.util.TimeOutSetting;
import com.cems.util.ShiroMd5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.*;
import java.util.concurrent.TimeUnit;


@Service
public class UserServiceImpl implements UserService {


    @Autowired
    RedisUtil redisUtil;
    @Autowired
    UserMapper userMapper;
    @Autowired
    EntrustMapper entrustMapper;

    // 获取所有委托的类型信息
    @Override
    public List<SysEntrust> getAllEtrustType() {
        try {
            //rides
            List<Object> objects = SelectRedis.selectRedis(redisUtil, "cems_AllEtrustType", entrustMapper, "getAllEtrustType");
            List<SysEntrust> list = new ArrayList<>();
            for (Object object : objects) {
                if (object instanceof SysEntrust)
                    list.add((SysEntrust) object);
            }
            return list;
        } catch (NoSuchMethodException | IllegalAccessException | InstantiationException | InvocationTargetException e) {
            e.printStackTrace();
            return entrustMapper.getAllEtrustType();
        }
    }

    @Override
    public List<SysEntrust> byEntrustByType(Integer typeID) {
        try {
            //rides
            List<Object> objects = SelectRedis.selectRedis(redisUtil, "cems_byEntrustByType", userMapper, "byEntrustByType");
            List<SysEntrust> list = new ArrayList<>();
            for (Object object : objects) {
                if (object instanceof SysEntrust)
                    list.add((SysEntrust) object);
            }
            return list;
        } catch (NoSuchMethodException | IllegalAccessException | InstantiationException | InvocationTargetException e) {
            e.printStackTrace();
            return userMapper.byEntrustByType(typeID);
        }
//        return userMapper.byEntrustByType(typeID);
    }

    //获取委托的信息

    @Override
    public List<SysEntrust> getEtrust() {
//        redisUtil.delete("cems_Entrust");
        try {
            //rides
            List<Object> objects = SelectRedis.selectRedis(redisUtil, "cems_Entrust", userMapper, "getEntrust");
            List<SysEntrust> list = new ArrayList<>();
            for (Object object : objects) {
                if (object instanceof SysEntrust)
                    list.add((SysEntrust) object);
            }
            return list;
        } catch (NoSuchMethodException | IllegalAccessException | InstantiationException | InvocationTargetException e) {
            e.printStackTrace();
            return userMapper.getEntrust();
        }
    }

    @Override
    public void upUserStatus(int id, String status) {
        userMapper.upUserStatus(id, status);
        redisUtil.delete("cems_AllUsers");
        redisUtil.delete("cems_AllUsers");
    }

    @Override
    public SysUser selOneSysUserByPhone(String userPhone) {
        return userMapper.selOneSysUserByPhone(userPhone);
    }

    //更待委托状态 并处理redis数据
    @Override
    public void upEtrustStatus(Integer entrustId, String entState) {
        userMapper.upEtrustStatus(entrustId, entState);
        redisUtil.delete("cems_LeisureEntrust");
        redisUtil.delete("cems_Entrust");

    }


    //获取用户详细信息  管理员使用
    @Override
    public List<SysUserMessage> getUserMessages() {
        try {
            //rides
            List<Object> objects = SelectRedis.selectRedis(redisUtil, "cems_UserMessages", userMapper, "getUserMessages");
            List<SysUserMessage> list = new ArrayList<>();
            for (Object object : objects) {
                if (object instanceof SysUserMessage)
                    list.add((SysUserMessage) object);
            }
            return list;
        } catch (NoSuchMethodException | IllegalAccessException | InstantiationException | InvocationTargetException e) {
            e.printStackTrace();
            return userMapper.getUserMessages();
        }
    }

    //用户注册
    @Override
    public void addUser(HashMap<String, String> map, String phone, String pwd, String emil) {
        //添加新注册的用户
        userMapper.addUser(map);
        //添加用户的信息  电话就是账号
        int adminIdByNum = userMapper.getUserIdByNum(phone);
        userMapper.addUserInformation(adminIdByNum, phone, pwd, emil, phone);
        redisUtil.delete("cems_AllUsers");
    }

    @Override
    public ComUser selOneUser(int useId) {
        return userMapper.selOneUser(useId);
    }

    @Override
    public SysUser selOneSysUser(int id) {
        return userMapper.selOneSysUser(id);
    }

    @Override
    public int revOneUser(Map<String, Object> map) {
        return userMapper.revOneUser(map);
    }

    @Override
    public int revOneSysUser(Map<String, Object> map) {
        int i = userMapper.revOneSysUser(map);
        redisUtil.delete("cems_UserMessages");
        redisUtil.delete("cems_AllUsers");
        return i;
    }


    @Override
    public List<SysUser> getUser() {
        try {
            //调用redis查询方法
            List<Object> objects = SelectRedis.selectRedis(redisUtil, "cems_AllUsers", userMapper, "getUser");
            ArrayList<SysUser> users = new ArrayList<>();
            for (Object object : objects) {
                if (object instanceof SysUser) {
                    users.add((SysUser) object);
                }
            }
            return users;
        } catch (NoSuchMethodException | IllegalAccessException | InstantiationException | InvocationTargetException e) {
            e.printStackTrace();
            return userMapper.getUser();
        }
    }

    @Override
    public void shensu(String phone, String content) {
        userMapper.shensu(phone, content);
    }

    @Override
    public String findStatus(String phone) {
        return userMapper.findStatus(phone);
    }

    @Override
    public int zhaohuicheck(String phone, String email) {
        return userMapper.zhaohuicheck(phone, email);
    }

    @Override
    public void zhaohuo(String phone, String newpwd1) {
        userMapper.zhaohuo(phone, newpwd1);
    }

    @Override
    public List<SysEcharts> selSysGraph(String adminType) {
        return userMapper.selSysGraph(adminType);
    }

    @Override
    public int setSysLoginGraph(Map<String, Object> map) {
        return userMapper.setSysLoginGraph(map);
    }

    @Override
    public SysEcharts selOneSysSuc(int id) {
        return userMapper.selOneSysSuc(id);
    }

    @Override
    public int insertSysSuc(int id) {
        return  userMapper.insertSysSuc(id);
    }


}
